<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      body {
        position: relative;
      }
      #box1 {
        width: 100px;
        height: 100px;
        background-color: red;
        position: absolute;
      }
    </style>
    <script>
      // 使div根据不同的方向键向不同的方向移动
      window.onload = function () {
        document.onkeydown = function (event) {
          var speed = 10;
          if (event.ctrlKey) {
            speed = 50;
          }
          switch (event.keyCode) {
            case 37:
              box1.style.left = box1.offsetLeft - speed + "px";
              break;
            case 39:
              box1.style.left = box1.offsetLeft + speed + "px";
              break;
            case 38:
              box1.style.top = box1.offsetTop - speed + "px";
              break;
            case 40:
              box1.style.top = box1.offsetTop + speed + "px";
              break;
          }
        };
      };
    </script>
  </head>
  <body>
    <div id="box1"></div>
  </body>
</html>
